package com.baihang.platform.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baihang.framework.controller.BaseController;
import com.baihang.framework.kit.JsonKit;
import com.baihang.framework.kit.PropKit;
import com.baihang.framework.kit.ShiroKit;
import com.baihang.framework.kit.StrKit;
import com.baihang.framework.kit.database.RedisKit;
import com.baihang.platform.service.IndexService;
import com.baihang.platform.shiro.User;

import redis.clients.jedis.Jedis;

@Controller
@RequestMapping("/")
public class IndexController extends BaseController {

	// private static Log log= LogFactory.getLog(IndexController.class);
	private static Logger logger = LoggerFactory.getLogger(IndexController.class);
	private Jedis jedis = RedisKit.getJedis();
	@Autowired
	private IndexService service;

	@RequestMapping
	public String index(Model model) {
		User user = ShiroKit.getCurrentUser();
		model.addAttribute("imageServer", PropKit.get("imageServer"));
		model.addAttribute("name", user.getRealName());
		model.addAttribute("idCard", user.getUserCode());
		model.addAttribute("HouseName", jedis.hmget("systemConfig","HouseName").get(0));
		model.addAttribute("HouseTel", jedis.hmget("systemConfig","HouseTel").get(0));
		if("1".equals(jedis.hmget("systemConfig","IsShowReadStartDate").get(0))) {
			model.addAttribute("ReadStartDate", jedis.hmget("systemConfig","ReadStartDate").get(0));			
		}else {
			model.addAttribute("ReadStartDate", PropKit.get("ReadStartDate"));
		}
		if("1".equals(jedis.hmget("systemConfig","IsShowReadEndDate").get(0))) {
			model.addAttribute("ReadEndDate", jedis.hmget("systemConfig","ReadEndDate").get(0));
		}else {
			model.addAttribute("ReadEndDate", PropKit.get("ReadEndDate"));
		}
		if("1".equals(jedis.hmget("systemConfig","IsShowReadTestStartDate").get(0))) {
			model.addAttribute("ReadTestStartDate", jedis.hmget("systemConfig","ReadTestStartDate").get(0));
		}else {
			model.addAttribute("ReadTestStartDate", PropKit.get("ReadTestStartDate"));
		}
		if("1".equals(jedis.hmget("systemConfig","IsShowReadTestEndDate").get(0))) {
			model.addAttribute("ReadTestEndDate", jedis.hmget("systemConfig","ReadTestEndDate").get(0));
		}else {
			model.addAttribute("ReadTestEndDate", PropKit.get("ReadTestEndDate"));
		}
		return "index";
	}

	@GetMapping("/login")
	public String login(Model model) {
		String state = jedis.hmget("systemConfig","State").get(0);
		if("0".equals(state)) {
			return "redirect:/finish";
		}
		System.err.println("验证类型=" + jedis.hmget("systemConfig","IsShowYZMorSFZ").get(0));
		// 验证类型,此值从配置文件中读取
		model.addAttribute("yzType", jedis.hmget("systemConfig","IsShowYZMorSFZ").get(0)); // 1 是 验证码验证 0是身份证验证
		model.addAttribute("LoginTest", jedis.hmget("systemConfig","LoginTest").get(0)); // 1 是 开启免密码登录 0是关闭
		model.addAttribute("StartTime", jedis.hmget("systemConfig","StartTime").get(0));
		model.addAttribute("ShoppingTime", jedis.hmget("systemConfig","ShoppingTime").get(0));
		model.addAttribute("imageServer", PropKit.get("imageServer"));
		return "login";
	}
	@Deprecated
	@PostMapping("/login")
	public String login(String D_Tel, String D_Code, String D_Card, Model model) {
		System.err.println(String.format("%s:%s:%s", D_Tel, D_Code, D_Card));
		if("1".equals(jedis.hmget("systemConfig","IsShowYZMorSFZ").get(0))) {//1 是验证码 0 是身份证
			if(StrKit.isEmpty(D_Tel) || StrKit.isEmpty(D_Code) || D_Code.length()!=4) {
				return "redirect:/";
			}			
		}else {
			if(StrKit.isEmpty(D_Tel) ||StrKit.isEmpty(D_Card)) {
				return "redirect:/";
			}		
		}
		//帐号是否被锁定判断
		if(jedis.sismember("suoding", D_Tel)) {
			logger.info("手机叫为{}的被锁定用户进行登录操作!",D_Tel);
			model.addAttribute("errorMsg", "您的帐号已被锁定");
			return "login";
		}
		// 判断是否在做登录操作
		if (service.login(D_Tel, D_Code, D_Card)) {

		} else {
			model.addAttribute("errorMsg", "帐号或用户名有误");
		}
		if (ShiroKit.isAuthed()) {
			return "redirect:/";
		}
		return "login";
	}
	
	@PostMapping(value = "/loginJson", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String loginJson(String D_Tel, String D_Code, String D_Card, Model model) {
		System.err.println(String.format("%s:%s:%s", D_Tel, D_Code, D_Card));
		if("1".equals(jedis.hmget("systemConfig","IsShowYZMorSFZ").get(0))) {
			if(StrKit.isEmpty(D_Tel) || StrKit.isEmpty(D_Code)) {
				return JsonKit.toDoubleJson("false", "手机号码或者验证码不能为空");
			}			
		}else {
			if(StrKit.isEmpty(D_Tel) ||StrKit.isEmpty(D_Card)) {
				return JsonKit.toDoubleJson("false", "手机号码或身份证号不能为空");
			}		
		}
		//帐号是否被锁定判断
		if(jedis.sismember("suoding", D_Tel)) {
			logger.info("手机叫为{}的被锁定用户进行登录操作!",D_Tel);
			return  JsonKit.toDoubleJson("false", "您的帐号已被锁定");
		}
		// 判断是否在做登录操作
		if (service.login(D_Tel, D_Code, D_Card)) {
			//进行认证成功相关操作，在用户表中加入登录日期
			service.updateLoginTime();
		} else {
			return JsonKit.toDoubleJson("false", "帐号或用户名有误");
		}
		if (ShiroKit.isAuthed()) {
			return JsonKit.toDoubleJson("true", "身份认证成功");
		}
		return JsonKit.toDoubleJson("true", "身份认证成功");
	}

	@RequestMapping("/logout")
	public String logout() {
		ShiroKit.logOut();
		return "redirect:/login";
	}

	@RequestMapping("/unAuthorized")
	public String unAuthorized() {

		return "unAuthorized";
	}

	@RequestMapping("/finish")
	public String finish() {
		String state = jedis.hmget("systemConfig","State").get(0);
		if(!"0".equals(state)) {
			return "redirect:/login";
		}
		return "finish";
	}

	@RequestMapping(value = "/sendCode", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String sendCode(String tel) {
		logger.info(tel);

		String result = "true";
		if (service.sendCode(tel)) {
			// 发送短信
		} else {
			result = "false";
		}
		logger.info(result);
		return String.format("{\"result\":\"%s\"}", result);
	}
	
}
